<!DOCTYPE html>
<html>
    <?php
    include_once "../DB/db.php";
    include_once '../class/CategoryClass.php';
    include_once '../Functions/imageFunctions.php';
    ?>
    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="CSS/mainFrame.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/general.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <!-- Display the main frame: -->
        <div class="wrapper">
            <?php
            include_once "../MainFrame/header.php";
            include_once "../MainFrame/menuBar.php";
            include_once "../MainFrame/sideBar.php";
            ?>

            <!-- The main category.php code lies within "content": -->
            <div class="content">
                <?php // Show admin choices if user is an admin:
                if($user->isLoggedOn() && $user->isAdmin()){
                    showAdminCategory(); // show admin choices
                 }?>
                <?php
                // Get the clicked category's name from sideBar.php
                $my_category = $_GET['cat'];            // Get the category name.
                $my_category = urldecode($my_category); // Decode the name
                $my_cat_copy = ucwords($my_category);   // Copy and beautify.
                ?>

                <?php
                // Find all the items connected to this category:
                $item_query = $cat->getCategoryItems($my_category);

                // Get all information about this category:
                $category_query = $cat->getCategoryInfo($my_category);
                ?>

                <!-- Display category info: -->
                <?php echo "<h1>$my_cat_copy</h1>"?>    <!-- Display header. -->
                <div id=category_description>       <!-- Display description: -->
                    <?php
                    $this_category = $category_query->fetch();
                    echo $this_category['description'];
                    ?>
                </div>

                <!-- Display all items in the category: -->
                <?php
                $counting = 0; // Helps to properly displaying category_table.
                ?>
                <table>
                    <?php
                    // Loop through all items in this category:
                    while ($row = $item_query->fetch()) {
                        $item_deleted = $row['deleted'];
                        $item_id = $row['vid'];
                        $item_name = $row['name'];
                        $name_copy = ucwords($item_name);
                        $item_description = substr($row['description'], 0, 50)."...";
                        $item_image = $row['picture'];  
                        $item_image = getImageFromBlob($item_image, 195, 200);
                        
                        // When MAX width of the webside is reached, make a lineshift:
                        if ($counting == 0) {
                            echo "<tr>";
                        }
                        ?>
                        <td>
                            <table id="category_table">
                                <tr>
                                    <th>
                                        <a href="item.php?itemId=<?php echo$item_id ?>">
                                            <?php echo $name_copy; ?></a>
                                    </th>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="item.php?itemId=<?php echo$item_id ?>">
                                            <?php echo $item_image; ?>
                                        </a>
                                    </td>
                                </tr>
                                <?php
                                echo "<tr><td height=64>$item_description</td></tr>";
                                // Create the "buy" button: ?>
                                <tr>
                                    <td height=26>
                                        <?php if($item_deleted==0){ // if item is not deleted:?>
                                        <form class="buyItem" method="GET" action="/Functions/cookieFunctions.php">
                                            <input name="antall" type="text" value="1" class="antall">
                                            <input name="vareNavn" type="hidden" value="<?php echo $row['vid'] ?>">
                                            <input name="vareKjop" type="submit" value="Kjop">
                                        </form>
                                        <?php } ?>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <?php
                        // When MAX width of the webside is reached, make a lineshift:
                        if ($counting == 3) {
                            echo "</tr>";
                            $counting = -1;
                        }
                        // Keep track of the width which is displayed in the website.
                        $counting++;
                    }
                    ?>
                </table>
            </div><br>

            <!-- Display the footer in the main frame. -->
        <?php include_once "../MainFrame/footer.php"; ?>
        </div>
    </body>



    <?php

    // Show admin choices Add item, and Change and Delete category.
    function showAdminCategory(){
        global $my_category;
        global $cat;
        $catInfo = $cat->getCategoryInfo($_GET['cat']);
        $catRow = $catInfo->fetch();
        ?>
        <div class="admin_category_table">
            <table>
                <tr>
                    <td><a href="/adminViews/adminItem.php?alterItem=newItem&catId=<?php echo $catRow['kid']; ?>">Legg til ny Vare</a></td>
                    <td><a href="/adminViews/adminCategory.php?alterCat=alterCat&catId=<?php echo $catRow['kid']; ?>">Endre Kategori</a></td>
                    <td><a href="/adminViews/adminCategory.php?alterCat=deleteCat&catId=<?php echo $catRow['kid']; ?>">Slett Kategori</a></td>
                </tr>
            </table>
        </div>
    <?php } ?>

</html>
